Editor.Util = {
  isDragging: function () {
    return Editor.STATE.DRAGGER != undefined && Editor.STATE.DRAGGER.isActive();
  },
  undo: function () {
    Editor.Util.unselectAll();
    Editor.STATE.CMDSTACK.undo()
  },
  redo: function () {
    Editor.Util.unselectAll();
    Editor.STATE.CMDSTACK.redo()
  },
  prepareAddResource: function () {         
    $("#lastType").val($("input[name='pmlclass']:checked").val());
    Editor.STATE.ADDSHAPE = Editor.Util.addResource;
    Editor.STATE.ADDDIALOG = 'dialogAddResource';
    Editor.Util.prepareAdd();
  },
  prepareAddPMLJ: function () {         
    $("#lastType").val($("input[name='pmlJclass']:checked").val());
    Editor.STATE.ADDSHAPE = Editor.Util.addPMLJ;
    Editor.STATE.ADDDIALOG = 'dialogAddResourceJ';
    Editor.Util.prepareAdd();
  },
  prepareAddPMLT: function () {         
    $("#lastType").val($("input[name='pmlTclass']:checked").val());
    Editor.STATE.ADDSHAPE = Editor.Util.addPMLT;
    Editor.STATE.ADDDIALOG = 'dialogAddResourceT';
    Editor.Util.prepareAdd();
  },
  prepareAddLiteral: function () {
    $("#lastType").val("");
    Editor.STATE.ADDSHAPE = Editor.Util.addLiteral;
    Editor.STATE.ADDDIALOG = 'dialogAddLiteral';
    Editor.Util.prepareAdd();
  },
  prepareAdd: function () {
    $('#canvasAdd').addClass('canvas');
    $('#canvasAdd').css("width",""+Editor.CONFIG.CANVAS_WIDTH);
    $('#canvasAdd').css("height",""+Editor.CONFIG.CANVAS_HEIGHT);
    Editor.STATE.ISADDSHAPE = true;
  },
  addResource: function (x, y, u, t) {
    var n1 = new Editor.Namespace();
    n1.init();
    n1.setPrefix('');
    n1.setUri('');
    var r1 = new Editor.ResourceConnectable();
    r1.init(u);
    if(t != null){
      if(t.type == 'blank'){
        r1.setBlank();
        //r1.setName("_:blank"+(Editor.BLANK.counter++));
      }  
      r1.setSuperType(t.superType);      
    }
    r1.setNamespace(n1);
    //r1.setName();
    r1.draw();
    r1.moveTo(x, y);
    Editor.STATE.SHAPES.store(r1.getUuid(),r1);
    return r1;
  },
  addPMLJ: function (x, y, u, t) {
    var n1 = new Editor.Namespace();
    n1.init();
    n1.setPrefix('');
    n1.setUri('');
    var r1 = new Editor.ResourceConnectable();
    r1.init(u);
    r1.setNamespace(n1);
    r1.setName('');
    r1.draw();
    r1.moveTo(x, y);
    r1.setSuperType(t);
    Editor.STATE.SHAPES.store(r1.getUuid(),r1);
    return r1;
  },
  addPMLT: function (x, y, u, t) {
    var n1 = new Editor.Namespace();
    n1.init();
    n1.setPrefix('');
    n1.setUri('');
    var r1 = new Editor.ResourceConnectable();
    r1.init(u);
    r1.setNamespace(n1);
    r1.setName('');
    r1.draw();
    r1.moveTo(x, y);
    r1.setSuperType(t);
    Editor.STATE.SHAPES.store(r1.getUuid(),r1);
    return r1;
  },
  addProperty: function (x, y, u, rIni, rEnd) {
    var ns = new Editor.Namespace();
    ns.init();
    var prop = new Editor.Property();
    prop.init(u,rIni,rEnd);
    prop.setNamespace(ns);
    prop.setName('');
    prop.draw();
    prop.moveTo(x, y);
    Editor.STATE.SHAPES.store(prop.getUuid(),prop);
    return prop;
  },
  addLiteral: function (x, y, u, o) {
    var l1 = new Editor.Literal();
    l1.init(u);
    l1.setText(o.text);
    l1.setLang(o.lang);
    l1.setDatatype(o.datatype);
    l1.draw();
    l1.moveTo(x, y);
    Editor.STATE.SHAPES.store(l1.getUuid(),l1);
    return l1;
  },
  addLiteral: function (x, y, u) {
    var l1 = new Editor.Literal();
    l1.init(u);
    l1.setText('');
    l1.setLang('');
    l1.setDatatype('');
    l1.draw();
    l1.moveTo(x, y);
    Editor.STATE.SHAPES.store(l1.getUuid(),l1);
    return l1;
  },
  unselectAll: function () {
    var arrSelected = Editor.STATE.SELECTED;
    while (arrSelected.length>0) {
      var shape = arrSelected.pop();
      shape.unselect();
    }
  },
  select: function (shape) {
    Editor.STATE.SELECTED.push(shape);
    shape.select();
  },
  unselect: function (shape) {
    var arrSelected = Editor.STATE.SELECTED;
    var toRemove =-1;
    for(var i=0;i<arrSelected.length;i++) {
      if (shape.getUuid() == arrSelected[i].getUuid()) {
        toRemove = i;
      }
    }
    if(toRemove>=0) {
      arrSelected.splice(toRemove,1);
    }
  },
  toggleLang: function () {
    if (Editor.STATE.LANG.code == 'es') {
      Editor.STATE.LANG = Editor.LANG.EN;
    } else if (Editor.STATE.LANG.code == 'en') {
      Editor.STATE.LANG = Editor.LANG.ES;
    }
    Editor.Init.lang();
  },
  canvasAddClick: function (e) {
    var pos = $("#canvas").position();
    var x = e.clientX - pos.left;
    var y = e.clientY - pos.top;
    if (Editor.CONFIG.GRID_SNAP) {
      x = Math.round(x / Editor.CONFIG.GRID_SIZE) * Editor.CONFIG.GRID_SIZE;
      y = Math.round(y / Editor.CONFIG.GRID_SIZE) * Editor.CONFIG.GRID_SIZE;
    }
    var cmd = new JS.Command({
                             execute: function () {
                             if (this.shape != undefined) {
                               this.shape.draw();
                             } else {
                               this.shape = Editor.STATE.ADDSHAPE(x, y);
                               Editor.STATE.SHAPETOUPDATE = this.shape;
                             }
                             },
                             undo: function () {
                               this.shape._remove();
                             },
                             stack: Editor.STATE.CMDSTACK
    });
    cmd.name = 'add shape';
    cmd.execute();
    Editor.STATE.ADDSHAPE = undefined;
    $('#canvasAdd').removeClass('canvas');
    $('#'+Editor.STATE.ADDDIALOG).jqm({overlay: 40, modal: true}).jqmShow();
  },
  updateResource: function (suffix) {
    var shape = Editor.STATE.SHAPETOUPDATE;
    var newName = $('#namespaceUri'+suffix).val();//$('#name').val();
    var newNamespacePrefix = $('#namespacePrefix'+suffix).val();
    var newNamespaceUri = $('#namespaceUri'+suffix).val();
    var type = $('#lastType').val();//'resource';
    if($("#isBlank"+suffix).is(":checked")){
      shape.setBlank();
      newName = "_:blank"+(Editor.BLANK.counter++);
    }else{
      shape.unsetBlank();
    }
    
    if (Editor.STATE.ISADDSHAPE) {
      shape.setName(newName);
      shape.getNamespace().setPrefix(newNamespacePrefix);
      shape.getNamespace().setUri(newNamespaceUri);
      shape.updateText();
      shape.setSuperType($('#lastType').val());
      Editor.STATE.ISADDSHAPE = false;
      if (shape.getTypeClass() == 'property') {
        var nW = -shape.getWidth()/2;
        var nH = -shape.getHeight()/2;
        if (Editor.CONFIG.GRID_SNAP) {
          nW = Math.round(nW / Editor.CONFIG.GRID_SIZE) * Editor.CONFIG.GRID_SIZE;
          nH = Math.round(nH / Editor.CONFIG.GRID_SIZE) * Editor.CONFIG.GRID_SIZE;
        }
        shape.translate(nW,nH);
        shape.drawLines();
      }
    } else {
      var oldName = shape.getName();
      var oldNamespacePrefix = shape.getNamespace().getPrefix();
      var oldNamespaceUri = shape.getNamespace().getUri();
      var oldWidth = shape.getWidth();
      var oldSuperType = type;
      var cmd = new JS.Command({
                               execute: function () {
                               shape.setName(newName);
                               shape.getNamespace().setPrefix(newNamespacePrefix);
                               shape.getNamespace().setUri(newNamespaceUri);
                               shape.updateText();
                               shape.setSuperType(type);
                               
                               },
                               undo: function () {
                                 shape.setName(oldName);
                                 shape.getNamespace().setPrefix(oldNamespacePrefix);
                                 shape.getNamespace().setUri(oldNamespaceUri);
                                 shape.updateText();
                                 shape._resize(oldWidth - shape.getWidth(),0);
                                 shape.setSuperType(oldSuperType);
                               },
                               stack: Editor.STATE.CMDSTACK
      });
      cmd.name = 'update resource';
      cmd.execute();
      if($("#isBlank"+suffix).is(":checked")){
        $("#isBlank"+suffix).attr('checked', false);
        $("#nonBlankNodes"+suffix).show('fast');                                      
      }
    }
    
    $('#dialogAddResource'+suffix).jqmHide();
    $('#name'+suffix).val("");
    $("#namespaceUri"+suffix).val("");
  },
  cancelUpdateResource: function () {
    if (Editor.STATE.ISADDSHAPE) {
      // tricky
      Editor.Util.undo();
      Editor.STATE.ISADDSHAPE = false;
    }
    $('#dialogAddResource').jqmHide();
  },
  updateProperty: function () {
    var shape = Editor.STATE.SHAPETOUPDATE;
    var newName = $('#propertyName :selected').text();
    var newUri = $('#propertyName :selected').val();
    var type = 'property';
    if (Editor.STATE.ISADDSHAPE) {
      shape.setName(newName);
      shape.getNamespace().setPrefix("");
      shape.getNamespace().setUri(newUri);
      shape.updateText();
      //shape.setSuperType(type);
      Editor.STATE.ISADDSHAPE = false;
	  var nW = -shape.getWidth()/2;
	  var nH = -shape.getHeight()/2;
	  if (Editor.CONFIG.GRID_SNAP) {
	    nW = Math.round(nW / Editor.CONFIG.GRID_SIZE) * Editor.CONFIG.GRID_SIZE;
	    nH = Math.round(nH / Editor.CONFIG.GRID_SIZE) * Editor.CONFIG.GRID_SIZE;
	  }
	  shape.translate(nW,nH);
	  shape.drawLines();
    } else {
      var oldName = shape.getName();
      var oldNamespacePrefix = shape.getNamespace().getPrefix();
      var oldNamespaceUri = shape.getNamespace().getUri();
      var oldWidth = shape.getWidth();
      //var oldSuperType = shape.getSuperType();
      newNamespacePrefix ="";
      newNamespaceUri="";
      var cmd = new JS.Command({
                               execute: function () {
                               shape.setName(newName);
                               shape.getNamespace().setPrefix("");
                               shape.getNamespace().setUri(newUri);
                               shape.updateText();
                               //shape.setSuperType(type);
                               
                               },
                               undo: function () {
                                 shape.setName(oldName);
                                 shape.getNamespace().setPrefix(oldNamespacePrefix);
                                 shape.getNamespace().setUri(oldNamespaceUri);
                                 shape.updateText();
                                 shape._resize(oldWidth - shape.getWidth(),0);
                                 shape.setSuperType(oldSuperType);
                               },
                               stack: Editor.STATE.CMDSTACK
      });
      cmd.name = 'update resource';
      cmd.execute();
    }
    $('#dialogAddProperty').jqmHide();
    $('#name').val("");
    $("#namespaceUri").val("");
  },
  cancelUpdateProperty: function () {
    if (Editor.STATE.ISADDSHAPE) {
      // tricky
      Editor.Util.undo();
      Editor.STATE.ISADDSHAPE = false;
    }
    $('#dialogAddProperty').jqmHide();
  },
  updateLiteral: function () {
    var shape = Editor.STATE.SHAPETOUPDATE;
    var newText = $('#text').val();
    var newLang = $('#lang').val();
    var newDatatype = $('#datatype').val();
    if (Editor.STATE.ISADDSHAPE) {
      shape.setText(newText);
      shape.setLang(newLang);
      shape.setDatatype(newDatatype);
      shape.updateText();
      Editor.STATE.ISADDSHAPE = false;
    } else {
      var oldText = shape.getText();
      var oldLang = shape.getLang();
      var oldDatatype = shape.getDatatype();
      var oldWidth = shape.getWidth();
      var oldHeight = shape.getHeight();
      var cmd = new JS.Command({
                               execute: function () {
                               shape.setText(newText);
                               shape.setLang(newLang);
                               shape.setDatatype(newDatatype);
                               shape.updateText();
                               },
                               undo: function () {
                                 shape.setText(oldText);
                                 shape.setLang(oldLang);
                                 shape.setDatatype(oldDatatype);
                                 shape.updateText();
                                 shape._resize(oldWidth - shape.getWidth(),oldHeight - shape.getHeight());
                               },
                               stack: Editor.STATE.CMDSTACK
      });
      cmd.name = 'update literal';
      cmd.execute();
    }
    $('#dialogAddLiteral').jqmHide();
  },
  cancelUpdateLiteral: function () {
    if (Editor.STATE.ISADDSHAPE) {
      // tricky
      Editor.Util.undo();
      Editor.STATE.ISADDSHAPE = false;
    }
    $('#dialogAddLiteral').jqmHide();
  },
  moveShapes: function (dir) {
    var step = 1;
    var dx = 0;
    var dy = 0;
    if (Editor.CONFIG.GRID_SNAP) {
      step = Editor.CONFIG.GRID_SIZE;
    }
    switch(dir) {
    case 'left':
      dx = step * -1;
      break;
    case 'right':
      dx = step;
      break;
    case 'up':
      dy = step * -1;
      break;
    case 'down':
      dy = step;
      break;
    }
    var arrSelected = Editor.STATE.SELECTED;
    var arr = new Array();
    for(var i=0;i<arrSelected.length;i++) {
      arr.push(arrSelected[i]);
    }
    var cmd = new JS.Command({
                             execute: function () {
                             for(var i=0;i<arr.length;i++) {
                               arr[i].translate(dx,dy);
                               if (arr[i].getTypeClass() == 'property') {
                                 arr[i].drawLines();
                               } else if (arr[i].getTypeClass() == "resource" || arr[i].getTypeClass() == "literal" || arr[i].getTypeClass() == "blank") {
                                 arr[i].updateLines();
                               }
                             }
                             },
                             undo: function () {
                               for(var i=0;i<arr.length;i++) {
                                 arr[i].translate(dx*-1,dy*-1);
                                 if (arr[i].getTypeClass() == 'property') {
                                   arr[i].drawLines();
                                 } else if (arr[i].getTypeClass() == "resource" || arr[i].getTypeClass() == "literal" || arr[i].getTypeClass() == "blank") {
                                   arr[i].updateLines();
                                 }
                               }
                             },
                             stack: Editor.STATE.CMDSTACK
    });
    cmd.name = 'move shapes';
    cmd.execute();
  },
  removeShapes: function () {
    var arrSelected = Editor.STATE.SELECTED;
    var arr = new Array();
    for(var i=0;i<arrSelected.length;i++) {
      arr.push(arrSelected[i]);
    }
    var arrProperties = new Array();
    for(var i=0;i<arrSelected.length;i++) {
      var s = arrSelected[i];
      if (s.getTypeClass() == "resource" || s.getTypeClass() == "literal") {
        var aProps = s.getInProperties();
        for(var j=0;j<aProps.length;j++) {
          var p = Editor.STATE.SHAPES.get(aProps[j]);
          if (!p.isDeleted()) {
            arrProperties.push(aProps[j]);
          }
        }
      }
    }
    var cmd = new JS.Command({
                             execute: function () {
                             for(var i=0;i<arr.length;i++) {
                               arr[i]._remove()
                             }
                             for(var j=0;j<arrProperties.length;j++) {
                               var p = Editor.STATE.SHAPES.get(arrProperties[j]);
                               if(!p.isDeleted()) {
                                 p._remove();
                               }
                             }
                             },
                             undo: function () {
                               for(var i=0;i<arr.length;i++) {
                                 arr[i].draw();
                               }
                               for(var j=0;j<arrProperties.length;j++) {
                                 var p = Editor.STATE.SHAPES.get(arrProperties[j]);
                                 if(p.isDeleted()) {
                                   p.draw();
                                 }
                               }
                             },
                             stack: Editor.STATE.CMDSTACK
    });
    cmd.name = 'remove shapes';
    cmd.execute();
  },
  showLines: function (max, text) {
    var arrtext = text.split(" ");
    var newline = "";
    var restext = [];
    var result = [];
    for(var i=0; i<arrtext.length; i++){
      if((newline+" "+arrtext[i]).length < Editor.CONFIG.MAX_WIDTH_LITERAL_BOX){
        newline += " "+arrtext[i];
      }else{
        restext.push(newline);
        newline = arrtext[i];
      }
    }
    if(newline != ""){
      restext.push(newline);
    }
    
    result[0] = restext.join("\n");
    result[1] = Editor.CONFIG.LITERAL_SHAPE_PER_LINE_FACTOR*Editor.Util.getHeight(text)*(restext.length);
    result[2] = 0;
    for(var i=0; i<restext.length; i++){
      if(result[2] < Editor.Util.getWidth(restext[i])){
        result[2] = Editor.Util.getWidth(restext[i]);
      }
    }
    return result;
      
  },
  addObject: function(obj) {
    var s;
    var dx = 0;
    var dy = 0;
    var bbox;
    if (obj.type == 'resource' || obj.type == 'blank') {
      bbox = obj.resource.shape.bbox;
      if(obj.type == 'resource'){
      bbox.width = Editor.Util.getWidth(obj.resource.element.name);
      }else{
        bbox.width = Editor.Util.getWidth("_:blank00");
      }
      s = Editor.Util.addResource(bbox.x,bbox.y,obj.resource.shape.uuid, obj);
    } else if (obj.type == 'literal') {
      bbox = obj.shape.bbox;
      if(Editor.Util.getWidth(obj.text) > Editor.CONFIG.MAX_WIDTH_LITERAL_BOX){
        var aux = Editor.Util.showLines(Editor.CONFIG.MAX_WIDTH_LITERAL_BOX, obj.text);
        obj.text = aux[0];
        bbox.width = aux[2];
        bbox.height = aux[1];
      }else{
        bbox.width = Editor.Util.getWidth(obj.text);
      }
      s = Editor.Util.addLiteral(bbox.x,bbox.y,obj.shape.uuid, obj);
      s.setText(obj.text);

      if (obj.lang) {s.setLang(obj.lang)};
      if (obj.datatype) {s.setDatatype(obj.datatype)};
    } else if (obj.type == 'property') {
      bbox = obj.resource.shape.bbox;
      t = obj.resource.element.name;
      t=t.replace("http://inference-web.org/2.0/pml-justification.owl#", "pmlj:");
      t=t.replace("http://inference-web.org/2.0/pml-provenance.owl#", "pmlp:");
      bbox.width = Editor.Util.getWidth(t);
      s = Editor.Util.addProperty(bbox.x,bbox.y,obj.resource.shape.uuid,obj.uuidIni,obj.uuidEnd);
    }
    if (obj.type == 'resource' || obj.type == 'property' || obj.type == 'blank') {	
      var ele = obj.resource.element;
      if(obj.type == 'blank'){
        s.setName('_:blank'+Editor.BLANK.counter++);
      }else{
        s.setName(ele.name);
      }
      var ns = ele.namespace;
      if (ns) {
        if (ns.prefix) {s.getNamespace().setPrefix(ns.prefix)}
        if (ns.uri) {s.getNamespace().setUri(ns.uri)}
      }
    }
    s.updateText();
    if (bbox.width) { dx = bbox.width-s.getWidth() }
    if (bbox.height) { dy = bbox.height-s.getHeight() }
    s._resize(dx,dy);
  },
  hideAllLines: function() {
    Editor.STATE.SHAPES.forEach(function(pair) {
                                if (!pair.value.isDeleted()) {
                                var shape = pair.value;
                                if (shape.getTypeClass() == 'property') {
                                  shape.hideLines();
                                }
                                }
    });
  },
  showAllLines: function() {
    Editor.STATE.SHAPES.forEach(function(pair) {
                                if (!pair.value.isDeleted()) {
                                var shape = pair.value;
                                if (shape.getTypeClass() == 'property') {
                                  shape.showLines();
                                }
                                }
    });
  },
  drawObjects: function (arrObjects) {
    for(i=0;i<arrObjects.length;i++) {
      obj = arrObjects[i];
      obj.superType=obj.rdftype;
      if (obj.type != 'property') {
        Editor.Util.addObject(obj);
      }
    }
    for(i=0;i<arrObjects.length;i++) {
      obj = arrObjects[i];
      if (obj.type == 'property') {
        Editor.Util.addObject(obj);
      }
    }
  },
  prepareOptions: function () {
    $('#dialogOptions').jqm({overlay: 40, modal: true}).jqmShow();
    $('#gridSize').val(Editor.CONFIG.GRID_SIZE);
    $('#canvasWidth').val(Editor.CONFIG.CANVAS_WIDTH);
    $('#canvasHeight').val(Editor.CONFIG.CANVAS_HEIGHT);
    if (Editor.CONFIG.GRID_SHOW) {
      $('#gridShowYes').attr('checked', true);
      $('#gridShowNo').attr('checked', false);
    } else {
      $('#gridShowYes').attr('checked', false);
      $('#gridShowNo').attr('checked', true);
    }
    if (Editor.CONFIG.GRID_SNAP) {
      $('#gridSnapYes').attr('checked', true);
      $('#gridSnapNo').attr('checked', false);
    } else {
      $('#gridSnapYes').attr('checked', false);
      $('#gridSnapNo').attr('checked', true);
    }
    
  },
  updateOptions: function () {
    Editor.CONFIG.GRID_SIZE = parseInt($('#gridSize').val());
    Editor.CONFIG.CANVAS_WIDTH = $('#canvasWidth').val();
    Editor.CONFIG.CANVAS_HEIGHT = $('#canvasHeight').val();
    Editor.STATE.CANVAS.setSize(Editor.CONFIG.CANVAS_WIDTH,Editor.CONFIG.CANVAS_HEIGHT);
    Editor.STATE.BACKGROUND0.attr({width: Editor.CONFIG.CANVAS_WIDTH, height:Editor.CONFIG.CANVAS_HEIGHT});
    Editor.STATE.BACKGROUND.attr({width: Editor.CONFIG.CANVAS_WIDTH, height:Editor.CONFIG.CANVAS_HEIGHT});
    $('#dialogOptions').jqmHide();
  },
  closeOptions: function () {
    $('#dialogOptions').jqmHide();
  },
  // http://www.netlobo.com/url_query_string_javascript.html
  gup: function (name) {
    name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
    var regexS = "[\\?&]"+name+"=([^&#]*)";
    var regex = new RegExp( regexS );
    var results = regex.exec( window.location.href );
    if( results == null )
      return "";
    else
      return results[1];
  },
  
  about: function () {
    $('#dialogAbout-'+Editor.STATE.LANG.code).jqm().jqmShow();
  },
  closeAbout: function () {
    $('#dialogAbout-'+Editor.STATE.LANG.code).jqmHide();
  },
  //do the same thing as the Perl "keys" subroutine
  getKeys: function (o) {
    var accumulator = [];
    for (var propertyName in o) {
      accumulator.push(propertyName);
    }
    return accumulator;
  },
  //get values instead of keys
  getValues: function (o) {
    var accumulator = [];
    for (var propertyName in o) {
      accumulator.push(o[propertyName]);
    }
    return accumulator;
  },
  blankDialogOn: function(){
    $("#nonBlankNodes").hide('fast');                                     
    $("#namespacePrefix").val("");
    $("#namespaceUri").val("");
    $("#name").val("");
    if(! $("#isBlank").is(':checked')){
      $("#isBlank").attr("checked", true);
    }
  },
  blankDialogOff: function(){
    $("#nonBlankNodes").show('fast');                                      
  },
  blankDialogPMLJOn: function(){
    $("#nonBlankNodesJ").hide('fast');                                     
    $("#namespacePrefixJ").val("");
    $("#namespaceUriJ").val("");
    $("#nameJ").val("");
    if(! $("#isBlankJ").is(':checked')){
      $("#isBlankJ").attr("checked", true);
    }
  },
  blankDialogPMLJOff: function(){
    $("#nonBlankNodesJ").show('fast');                                      
  },
  blankDialogPMLTOn: function(){
    $("#nonBlankNodesT").hide('fast');                                     
    $("#namespacePrefixT").val("");
    $("#namespaceUriT").val("");
    $("#nameT").val("");
    if(! $("#isBlankT").is(':checked')){
      $("#isBlankT").attr("checked", true);
    }
  },
  blankDialogPMLTOff: function(){
    $("#nonBlankNodesT").show('fast');                                      
  },
  updatePMLP: function(){
    Editor.Util.updateResource("");
  },
  cancelUpdatePMLJ: function () {
    if (Editor.STATE.ISADDSHAPE) {
      // tricky
      Editor.Util.undo();
      Editor.STATE.ISADDSHAPE = false;
    }
    $('#dialogAddResourceJ').jqmHide();
  },
  updatePMLJ: function () {
    Editor.Util.updateResource("J");
  },
  cancelUpdatePMLT: function () {
    if (Editor.STATE.ISADDSHAPE) {
      // tricky
      Editor.Util.undo();
      Editor.STATE.ISADDSHAPE = false;
    }
    $('#dialogAddResourceT').jqmHide();
  },  
  updatePMLT: function () {
    Editor.Util.updateResource("T");
  },
  getWidth: function(text){
    var ruler = document.getElementById("ruler");
    ruler.innerHTML = text;
    return ruler.offsetWidth;
  },
  getHeight: function(text){
    var ruler = document.getElementById("ruler");
    ruler.innerHTML = text;
    return ruler.offsetHeight;
  },
  validate: function(){
    $("#buttonValidate").text("Validating...");
    $("#buttonValidate").attr("disabled", "true");
    $.post('validator.php', {'j':Editor.File.toJson()}, function(data){
           var message="<p class='sectiontitle'>Results</p>";
           var warncount=0;
           var errorcount=0;
           var fatalcount=0;
           
           var warntext = "";
           var errortext = "";
           var fataltext = "";
           $("#spanwarnings").html("");
           $("#spanerrors").html("");
           $("#spanfatals").html("");
           
           for(i in data.report){
             if(data.report[i].level == 'warn'){
               warncount++;
               warntext += "- "+data.report[i].details+"\n\n";
             }
             if(data.report[i].level == 'error'){
               errorcount++;
               errortext += "- "+data.report[i].details+"\n\n";
             }
             if(data.report[i].level == 'fatal'){
               fatalcount++;
               fataltext += "- "+data.report[i].details+"\n\n";
             }
           }
           if(errorcount==0 && fatalcount==0){//if(data.result == "passed"){
             alert("Congratulations! Your PML graph is valid!");
           }else{
             alert("Sorry, there is one or more errors in your PML");
           }
           if(warncount>0){
             message += "<img src='img/warn.png' class='validationsign'><button id='warnings' class='resourcewarning'>"+warncount+" Warnings</button><br />";
             $("#spanwarnings").html(warntext);
           }
           if(errorcount>0){
             message += "<img src='img/error.png' class='validationsign'/><button id='errors'>"+errorcount+" Errors</button><br />";
             $("#spanerrors").html(errortext);
           }
           if(fatalcount>0){
             message = "<img src='img/fatal.png' class='validationsign'/><button id='fatals'>"+fatalcount+" Fatal errors</button><br />";
             $("#spanfatals").html(fataltext);
           }
           $('#validation_results').html(message);
           $("#buttonValidate").text(Editor.STATE.LANG.ADD_VALID);
           $("#buttonValidate").removeAttr("disabled");
           
           
    },   'json');   

  },
}
